| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- <template>
- <div class="admin--popup-form">
- <div v-if="isLoading" class="admin--loading">
- 데이터를 불러오는 중...
- </div>
- <form v-else @submit.prevent="handleSubmit" class="admin--form">
- <!-- 형태 -->
- <div class="admin--form-group">
- <label class="admin--form-label">형태 <span class="admin--required">*</span></label>
- <div class="admin--radio-group">
- <label class="admin--radio-label">
- <input
- v-model="formData.type"
- type="radio"
- value="html"
- name="type"
- >
- <span>HTML</span>
- </label>
- <label class="admin--radio-label">
- <input
- v-model="formData.type"
- type="radio"
- value="image"
- name="type"
- >
- <span>단일페이지</span>
- </label>
- </div>
- </div>
- <!-- 제목 -->
- <div class="admin--form-group">
- <label class="admin--form-label">제목 <span class="admin--required">*</span></label>
- <input
- v-model="formData.title"
- type="text"
- class="admin--form-input"
- placeholder="팝업 제목을 입력하세요"
- required
- >
- </div>
- <!-- 시작일/종료일 -->
- <div class="admin--form-group">
- <label class="admin--form-label">시작일/종료일 <span class="admin--required">*</span></label>
- <div class="admin--date-range">
- <input
- v-model="formData.start_date"
- type="date"
- class="admin--form-input"
- required
- >
- <span class="admin--date-separator">~</span>
- <input
- v-model="formData.end_date"
- type="date"
- class="admin--form-input"
- required
- >
- </div>
- </div>
- <!-- 팝업창 사이즈 -->
- <div class="admin--form-group">
- <label class="admin--form-label">팝업창 사이즈 <span class="admin--required">*</span></label>
- <div class="admin--size-group">
- <div class="admin--size-item">
- <label>가로</label>
- <input
- v-model.number="formData.width"
- type="number"
- class="admin--form-input"
- placeholder="800"
- min="100"
- required
- >
- <span>px</span>
- </div>
- <div class="admin--size-item">
- <label>세로</label>
- <input
- v-model.number="formData.height"
- type="number"
- class="admin--form-input"
- placeholder="600"
- min="100"
- required
- >
- <span>px</span>
- </div>
- </div>
- </div>
- <!-- 팝업창 위치 -->
- <div class="admin--form-group">
- <label class="admin--form-label">팝업창 위치 <span class="admin--required">*</span></label>
- <div class="admin--size-group">
- <div class="admin--size-item">
- <label>TOP</label>
- <input
- v-model.number="formData.position_top"
- type="number"
- class="admin--form-input"
- placeholder="100"
- min="0"
- required
- >
- <span>px</span>
- </div>
- <div class="admin--size-item">
- <label>LEFT</label>
- <input
- v-model.number="formData.position_left"
- type="number"
- class="admin--form-input"
- placeholder="100"
- min="0"
- required
- >
- <span>px</span>
- </div>
- </div>
- </div>
- <!-- 쿠키설정 -->
- <div class="admin--form-group">
- <label class="admin--form-label">쿠키설정</label>
- <div class="admin--radio-group">
- <label class="admin--radio-label">
- <input
- v-model="formData.cookie_type"
- type="radio"
- value="today"
- name="cookie_type"
- >
- <span>오늘 하루 창 띄우지 않음</span>
- </label>
- <label class="admin--radio-label">
- <input
- v-model="formData.cookie_type"
- type="radio"
- value="forever"
- name="cookie_type"
- >
- <span>다시는 창을 띄우지 않음</span>
- </label>
- <label class="admin--radio-label">
- <input
- v-model="formData.cookie_type"
- type="radio"
- value="none"
- name="cookie_type"
- >
- <span>사용 안 함</span>
- </label>
- </div>
- </div>
- <!-- 출력내용 (HTML) -->
- <div v-if="formData.type === 'html'" class="admin--form-group">
- <label class="admin--form-label">출력내용 <span class="admin--required">*</span></label>
- <SunEditor
- v-model="formData.content"
- height="400px"
- placeholder="팝업 내용을 입력하세요"
- />
- </div>
- <!-- 출력내용 (이미지) -->
- <div v-if="formData.type === 'image'" class="admin--form-group">
- <label class="admin--form-label">이미지 첨부 <span class="admin--required">*</span></label>
- <input
- type="file"
- accept="image/*"
- class="admin--form-file"
- @change="handleImageUpload"
- >
- <div v-if="imagePreview || formData.image_url" class="admin--image-preview">
- <img :src="imagePreview || formData.image_url" alt="미리보기">
- <button
- type="button"
- class="admin--btn-remove-image"
- @click="removeImage"
- >
- 삭제
- </button>
- </div>
- </div>
- <!-- 링크 URL (단일페이지일 경우) -->
- <div v-if="formData.type === 'image'" class="admin--form-group">
- <label class="admin--form-label">링크 URL</label>
- <input
- v-model="formData.link_url"
- type="url"
- class="admin--form-input"
- placeholder="https://example.com"
- >
- </div>
- <!-- 버튼 영역 -->
- <div class="admin--form-actions">
- <button
- type="submit"
- class="admin--btn admin--btn-primary"
- :disabled="isSaving"
- >
- {{ isSaving ? '저장 중...' : '확인' }}
- </button>
- <button
- type="button"
- class="admin--btn admin--btn-secondary"
- @click="goToList"
- >
- 목록
- </button>
- </div>
- <!-- 성공/에러 메시지 -->
- <div v-if="successMessage" class="admin--alert admin--alert-success">
- {{ successMessage }}
- </div>
- <div v-if="errorMessage" class="admin--alert admin--alert-error">
- {{ errorMessage }}
- </div>
- </form>
- </div>
- </template>
- <script setup>
- import { ref, onMounted } from 'vue'
- import { useRoute, useRouter } from 'vue-router'
- import SunEditor from '~/components/admin/SunEditor.vue'
- definePageMeta({
- layout: 'admin',
- middleware: ['auth']
- })
- const route = useRoute()
- const router = useRouter()
- const { get, put, upload } = useApi()
- const isLoading = ref(true)
- const isSaving = ref(false)
- const successMessage = ref('')
- const errorMessage = ref('')
- const imagePreview = ref(null)
- const imageFile = ref(null)
- const formData = ref({
- type: 'html',
- title: '',
- start_date: '',
- end_date: '',
- width: 800,
- height: 600,
- position_top: 100,
- position_left: 100,
- cookie_type: 'none',
- content: '',
- image_url: '',
- link_url: ''
- })
- // 데이터 로드
- const loadPopup = async () => {
- isLoading.value = true
- const id = route.params.id
- const { data, error } = await get(`/basic/popup/${id}`)
- if (data) {
- formData.value = {
- type: data.type || 'html',
- title: data.title || '',
- start_date: data.start_date || '',
- end_date: data.end_date || '',
- width: data.width || 800,
- height: data.height || 600,
- position_top: data.position_top || 100,
- position_left: data.position_left || 100,
- cookie_type: data.cookie_type || 'none',
- content: data.content || '',
- image_url: data.image_url || '',
- link_url: data.link_url || ''
- }
- }
- isLoading.value = false
- }
- // 이미지 업로드
- const handleImageUpload = (event) => {
- const file = event.target.files[0]
- if (!file) return
- if (!file.type.startsWith('image/')) {
- alert('이미지 파일만 업로드 가능합니다.')
- return
- }
- imageFile.value = file
- // 미리보기
- const reader = new FileReader()
- reader.onload = (e) => {
- imagePreview.value = e.target.result
- }
- reader.readAsDataURL(file)
- }
- // 이미지 삭제
- const removeImage = () => {
- imagePreview.value = null
- imageFile.value = null
- formData.value.image_url = ''
- }
- // 폼 제출
- const handleSubmit = async () => {
- successMessage.value = ''
- errorMessage.value = ''
- // 유효성 검사
- if (!formData.value.title) {
- errorMessage.value = '제목을 입력하세요.'
- return
- }
- if (!formData.value.start_date || !formData.value.end_date) {
- errorMessage.value = '시작일과 종료일을 선택하세요.'
- return
- }
- if (formData.value.type === 'html' && !formData.value.content) {
- errorMessage.value = '출력내용을 입력하세요.'
- return
- }
- if (formData.value.type === 'image' && !imageFile.value && !formData.value.image_url) {
- errorMessage.value = '이미지를 첨부하세요.'
- return
- }
- isSaving.value = true
- try {
- let imageUrl = formData.value.image_url
- // 이미지 업로드 (새로운 이미지가 있는 경우)
- if (formData.value.type === 'image' && imageFile.value) {
- const formDataImage = new FormData()
- formDataImage.append('image', imageFile.value)
- const { data: uploadData, error: uploadError } = await upload('/upload/image', formDataImage)
- if (uploadError) {
- errorMessage.value = '이미지 업로드에 실패했습니다.'
- isSaving.value = false
- return
- }
- imageUrl = uploadData.url
- }
- // 팝업 수정
- const submitData = {
- ...formData.value,
- image_url: imageUrl
- }
- const id = route.params.id
- const { data, error } = await put(`/basic/popup/${id}`, submitData)
- if (error) {
- errorMessage.value = error.message || '수정에 실패했습니다.'
- } else {
- successMessage.value = '팝업이 수정되었습니다.'
- setTimeout(() => {
- router.push('/admin/basic/popup')
- }, 1000)
- }
- } catch (error) {
- errorMessage.value = '서버 오류가 발생했습니다.'
- console.error('Save error:', error)
- } finally {
- isSaving.value = false
- }
- }
- // 목록으로 이동
- const goToList = () => {
- router.push('/admin/basic/popup')
- }
- onMounted(() => {
- loadPopup()
- })
- </script>
|